package com.cap.dreamcircle.View.Adapter.Item;

import android.app.Activity;
import android.content.Intent;
import android.support.v4.app.ActivityCompat;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cap.dreamcircle.Common.RecyclerView.BaseItem;
import com.cap.dreamcircle.Consts.Constants;
import com.cap.dreamcircle.Model.Action.FocusAction;
import com.cap.dreamcircle.Model.Bean.NotifyEntity;
import com.cap.dreamcircle.R;
import com.cap.dreamcircle.Utils.GlideUtils;
import com.cap.dreamcircle.Utils.KeyCounterUtils;
import com.cap.dreamcircle.View.ArticalWebviewActivity;
import com.cap.dreamcircle.View.DreamDetailActivity;
import com.cap.dreamcircle.View.PersonalInfoActivity;

import de.greenrobot.event.EventBus;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by jihongwen on 16/6/30.
 */

public class DreamNotifyItem extends BaseItem<NotifyEntity> implements View.OnClickListener {

    NotifyEntity resultsBean;
    private CircleImageView imv_notify_header;
    private TextView notify_user_name;
    private TextView tv_notify_time;
    private TextView tv_notify_tips;
    private ImageView imv_voice;
    private TextView tv_dream_main_content;
    private TextView tv_response;

    private TextView tv_comment_num;
    private RelativeLayout layout_audio;
    private ImageView imv_playing_status_audio;
    private ImageView imv_etc_menu;


    public DreamNotifyItem(ViewGroup parent) {
        super(parent);
    }

    @Override
    public int getRes() {
        return R.layout.adapter_notify;
    }

    @Override
    public void onCreateView(View rootView) {
        imv_notify_header = (CircleImageView) rootView.findViewById(R.id.imv_notify_header);
        notify_user_name = (TextView) rootView.findViewById(R.id.notify_user_name);
        tv_notify_time = (TextView) rootView.findViewById(R.id.tv_notify_time);
        tv_notify_tips = (TextView) rootView.findViewById(R.id.tv_notify_tips);
        imv_voice = (ImageView) rootView.findViewById(R.id.imv_voice);
        tv_dream_main_content = (TextView) rootView.findViewById(R.id.tv_dream_main_content);
        tv_response=(TextView)rootView.findViewById(R.id.tv_response);
    }

    @Override
    public void onBindView(final NotifyEntity response) {
        this.resultsBean = response;
        notify_user_name.setText(this.resultsBean.getFromUserName());
        tv_notify_time.setText(this.resultsBean.getCreateTime());
        if(!TextUtils.isEmpty(this.resultsBean.getDreamVoiceUrl())){
            imv_voice.setVisibility(View.VISIBLE);
            tv_dream_main_content.setVisibility(View.GONE);
        }
        else if(!TextUtils.isEmpty(this.resultsBean.getDreamText())){
            imv_voice.setVisibility(View.GONE);
            tv_dream_main_content.setVisibility(View.VISIBLE);
            tv_dream_main_content.setText(this.resultsBean.getDreamText());
        }
        else{
            imv_voice.setVisibility(View.GONE);
            tv_dream_main_content.setVisibility(View.GONE);
        }

        if("jubaoDream".equals(this.resultsBean.getMsgType())){
            imv_notify_header.setImageResource(R.mipmap.report_notify_icon);
        }
        else{
            GlideUtils.getInstance().LoadContextBitmap(getContext(), Constants.BASE_URL + resultsBean.getFromUserHeadUrl(), imv_notify_header, R.mipmap.default_header, R.mipmap.default_header, GlideUtils.LOAD_BITMAP);
        }

        if(this.resultsBean.getMsgType().equals("likeDream")){
            tv_notify_tips.setVisibility(View.VISIBLE);
            tv_notify_tips.setText("赞了你的梦境");
        }
        if(this.resultsBean.getMsgType().equals("commentDream")){
            tv_notify_tips.setVisibility(View.VISIBLE);
            tv_notify_tips.setText("评论了你: "+response.getCommentContent());
            tv_response.setVisibility(View.VISIBLE);
            tv_response.setText("回复");
        }
        if("commentReplyAd".equals(this.resultsBean.getMsgType())){
            tv_notify_tips.setVisibility(View.VISIBLE);
            tv_notify_tips.setText("评论了你: "+response.getCommentContent());
            imv_voice.setVisibility(View.GONE);
            tv_dream_main_content.setVisibility(View.VISIBLE);
            tv_dream_main_content.setText(this.resultsBean.getAd().getTitle());
            tv_response.setVisibility(View.VISIBLE);
            tv_response.setText("回复");
        }
        if("commentReply".equals(this.resultsBean.getMsgType())){
            tv_notify_tips.setVisibility(View.VISIBLE);
            tv_notify_tips.setText("评论了你: "+response.getCommentContent());
            tv_response.setVisibility(View.VISIBLE);
            tv_response.setText("回复");
        }
        if("follow".equals(this.resultsBean.getMsgType())){
            tv_notify_tips.setVisibility(View.VISIBLE);
            tv_notify_tips.setText("关注了你");
            tv_response.setVisibility(View.VISIBLE);
            if("n".equals(resultsBean.getFollowAlready())){
                tv_response.setText("关注他");
            }
            else{
                tv_response.setText("已关注");
            }
            tv_response.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    FocusAction action=new FocusAction();
                    action.setUserId(resultsBean.getFromUserId());
                    action.setPosition(getPosition());
                    if("n".equals(resultsBean.getFollowAlready())){
                        action.setFocus(true);
                    }
                    else{
                        action.setFocus(false);
                    }
                    EventBus.getDefault().post(action);
                }
            });
        }
        if("modifyUserInfo".equals(this.resultsBean.getMsgType())){
            tv_notify_tips.setVisibility(View.VISIBLE);
            tv_notify_tips.setText("完善了资料");
            tv_response.setVisibility(View.VISIBLE);
            tv_response.setText("查看");
        }
        imv_notify_header.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(resultsBean.getMsgType().equals("likeDream")){
                    Intent intent =new Intent(getContext(), PersonalInfoActivity.class);
                    intent.putExtra("userId",resultsBean.getFromUserId());
                    ActivityCompat.startActivity((Activity) getContext(), intent, null);
                }
                if(resultsBean.getMsgType().equals("commentDream")){
                    if("y".equals(resultsBean.getShowName())){
                        Intent intent =new Intent(getContext(), PersonalInfoActivity.class);
                        intent.putExtra("userId",resultsBean.getFromUserId());
                        ActivityCompat.startActivity((Activity) getContext(), intent, null);
                    }
                }
                if("commentReplyAd".equals(resultsBean.getMsgType())){
                    if("y".equals(resultsBean.getShowName())){
                        Intent intent =new Intent(getContext(), PersonalInfoActivity.class);
                        intent.putExtra("userId",resultsBean.getFromUserId());
                        ActivityCompat.startActivity((Activity) getContext(), intent, null);
                    }
                }
                if("commentReply".equals(resultsBean.getMsgType())){
                    if("y".equals(resultsBean.getShowName())){
                        Intent intent =new Intent(getContext(), PersonalInfoActivity.class);
                        intent.putExtra("userId",resultsBean.getFromUserId());
                        ActivityCompat.startActivity((Activity) getContext(), intent, null);
                    }
                }
                if("follow".equals(resultsBean.getMsgType())){
                    Intent intent =new Intent(getContext(), PersonalInfoActivity.class);
                    intent.putExtra("userId",resultsBean.getFromUserId());
                    ActivityCompat.startActivity((Activity) getContext(), intent, null);
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        if(this.resultsBean.getMsgType().equals("likeDream")){
            KeyCounterUtils.SendKeyCounter("cf11e14d98e54682880a9a5f3143a4d9");
            Intent intent = new Intent(getContext(), DreamDetailActivity.class);
            intent.putExtra("dreamId",resultsBean.getDreamId()+"");
            intent.putExtra("voiceUrl",resultsBean.getDreamVoiceUrl());
            getContext().startActivity(intent);
        }
        if(this.resultsBean.getMsgType().equals("commentDream")){
            KeyCounterUtils.SendKeyCounter("cf11e14d98e54682880a9a5f3143a4d9");
            Intent intent = new Intent(getContext(), DreamDetailActivity.class);
            intent.putExtra("dreamId",resultsBean.getDreamId()+"");
            intent.putExtra("voiceUrl",resultsBean.getDreamVoiceUrl());
            getContext().startActivity(intent);
        }
        if("commentReplyAd".equals(this.resultsBean.getMsgType())){
            KeyCounterUtils.SendKeyCounter("c449fa35fff24b058a4d5d5e00582d6b");
            Intent intent=new Intent(getContext(), ArticalWebviewActivity.class);
            intent.putExtra("webview_title",this.resultsBean.getAd().getTitle());
            intent.putExtra("webview_content",this.resultsBean.getAd().getContent());
            intent.putExtra("AdId",this.resultsBean.getAd().getId());
            intent.putExtra("ad_likes",this.resultsBean.getAd().hasLiked());
            intent.putExtra("ad_comment_num",this.resultsBean.getAd().getCommentCount());
            intent.putExtra("ad_like_num",this.resultsBean.getAd().getLikeCount());
            getContext().startActivity(intent);
        }
        if("commentReply".equals(this.resultsBean.getMsgType())){
            KeyCounterUtils.SendKeyCounter("cf11e14d98e54682880a9a5f3143a4d9");
            Intent intent = new Intent(getContext(), DreamDetailActivity.class);
            intent.putExtra("dreamId",resultsBean.getDreamId()+"");
            intent.putExtra("voiceUrl",resultsBean.getDreamVoiceUrl());
            getContext().startActivity(intent);
        }
        if("follow".equals(this.resultsBean.getMsgType())){
            Intent intent =new Intent(getContext(), PersonalInfoActivity.class);
            intent.putExtra("userId",resultsBean.getFromUserId());
            ActivityCompat.startActivity((Activity) getContext(), intent, null);
        }
        if("modifyUserInfo".equals(resultsBean.getMsgType())){
            Intent intent =new Intent(getContext(), PersonalInfoActivity.class);
            intent.putExtra("userId",resultsBean.getFromUserId());
            ActivityCompat.startActivity((Activity) getContext(), intent, null);
        }
    }
}
